// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package dengues.system;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.EntityReference;
import org.w3c.dom.Text;

public class HTMLWebHelper {

    private static org.w3c.tidy.Tidy tidy = null;

    static {
        tidy = new org.w3c.tidy.Tidy();
        tidy.setXHTML(true);
        tidy.setQuiet(true);
        tidy.setXmlOut(true);
        tidy.setDropFontTags(true);
        tidy.setDropEmptyParas(true);
        tidy.setFixComments(true);
        tidy.setFixBackslash(true);
        tidy.setMakeClean(true);
        tidy.setQuoteNbsp(true);
        tidy.setQuoteMarks(true);
        tidy.setQuoteAmpersand(true);
        tidy.setShowWarnings(false);
        tidy.setEncloseText(true);
        tidy.setEncloseBlockText(true);

    }

    public static Document parseDOM(InputStream is) {
        return parseDOMUseTidy(is, null);
    }

    // public static Document parseDOMUseNekohtml(InputStream is) {
    // org.cyberneko.html.parsers.DOMParser parser = new org.cyberneko.html.parsers.DOMParser();
    // try {
    // parser.parse(new InputSource(is));
    // return parser.getDocument();
    // } catch (SAXException ex) {
    // ex.printStackTrace();
    // } catch (IOException ex) {
    // ex.printStackTrace();
    // }
    // return null;
    //
    // }

    public static Document parseDOMUseTidy(InputStream is, OutputStream os) {
        return tidy.parseDOM(is, os);
    }

    public static String convert(String src) {
        if (src == null) {
            return "";
        } else {
            try {
                return new String(src.getBytes("ISO8859-1"), "GB2312");
            } catch (UnsupportedEncodingException e) {
                return "";
            }
        }
    }

    public static String getContent(org.w3c.dom.Node n) {
        StringBuffer buf = new StringBuffer();
        if (n == null) {
            return buf.toString();
        }
        getContent(n, buf);
        return buf.toString();
    }

    private static void getContent(org.w3c.dom.Node node, StringBuffer buf) {
        for (org.w3c.dom.Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) {
            if ((c instanceof Element) || (c instanceof EntityReference)) {
                getContent(c, buf);
            } else if (c instanceof Text) {
                buf.append(c.getNodeValue());

            }
        }
    }

    public static String getAttr(org.w3c.dom.Node node, String attrName, String defaultValue) {
        if (node instanceof Document) {
            node = ((Document) node).getDocumentElement();
        }
        String v = null;
        if (node instanceof Element) {
            v = ((Element) node).getAttribute(attrName);
        }
        if (v == null || v.equals("")) {
            return defaultValue;
        } else {
            return v;
        }
    }

}
